package solitaire.presentation;

import java.awt.Point;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;

import javax.swing.JComponent;
import javax.swing.JPanel;

import solitaire.InterfacesControle.ICTasDeCartes;
import solitaire.InterfacesPresentation.IPCarte;
import solitaire.InterfacesPresentation.IPTas;
import solitaire.InterfacesPresentation.IPTasDeCartesColorees;
import solitaire.controle.CTasDeCartesColorees;


public class PTasDeCartesColorees extends JPanel implements IPTasDeCartesColorees, DropTargetListener {
	private ICTasDeCartes controlleur;
	private DropTarget dt;
	
	public PTasDeCartesColorees(CTasDeCartesColorees control){
		controlleur=control;
		setLayout(new OverlapLayout(new Point(0, 0)));
		dt=new DropTarget(getJComponent(), 3, this, true, null);
	}
	
	
	@Override
	public JComponent getJComponent() {
		return this;
	}


	@Override
	public void setControleur(ICTasDeCartes cTasDeCartes) {
		controlleur=cTasDeCartes;
		
	}


	@Override
	public void depiler() {
		repaint();
		
	}


	@Override
	public void empiler(IPCarte carte) {
		add(carte.getJPanel());
		repaint();
	}

	
	@Override
	public void empiler(IPTas tas) {
		add(tas.getJComponent());
		repaint();
	}

	@Override
	public ICTasDeCartes getControleur() {
		return controlleur;
	}


	@Override
	public void dragEnter(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dragExit(DropTargetEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void dragOver(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void drop(DropTargetDropEvent arg) {
		ICTasDeCartes tasTransferable=null;
		try {
			tasTransferable=(ICTasDeCartes) arg.getTransferable().getTransferData(arg.getCurrentDataFlavorsAsList().get(0));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		//impossible d'empiler plus d'une carte sur un tas colore.
		if(tasTransferable.getNombre()!=1 || !controlleur.isEmpilable(tasTransferable.getICCarteSommet())){
			arg.rejectDrop();
		}
		else{
			arg.acceptDrop(DnDConstants.ACTION_MOVE);
			controlleur.empiler(tasTransferable);
			arg.dropComplete(true);
		}
		repaint();
		
	}


	@Override
	public void dropActionChanged(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
